﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
using TVXD23.Web.Models;
using TVXD23.Web.Models.ViewModel;

namespace TVXD23.Web.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        cmsRepository r = new cmsRepository();
        private IMGRepository Repository = new IMGRepository();
        [HandleError]
        public ActionResult Index(string lang)
        {
         
           
           // Roles.AddUserToRole("phungtd2","Super Admin");
            HomeIndexViewModel model = new HomeIndexViewModel();
            if (lang == "en")
            {

                var kq = r.GetFirstPressTranslatedCopyByCategoryId(2);
             
                if (kq != null)
                {
                    model.AboutCompany = new AboutCompanyViewModel();
                    model.AboutCompany.Title = kq.Title;
                    model.AboutCompany.Body = kq.Body;
                    model.AboutCompany.Description = kq.Description;
                    model.AboutCompany.PressId = kq.PressId;
                  
                }
                else model.AboutCompany = null;
                model.ImagesSlide = Repository.SlideImages();
                return View("~/Views/Home/TranslatedCopy/Index.aspx", model);

            }
            var kq2 = r.GetFirstPressByCategoryId(2);
           // var kq2 = r.GetPress("Giới thiệu công ty TVXD 23");
        
            if (kq2 != null)
            {
                model.AboutCompany = new AboutCompanyViewModel();
                model.AboutCompany.Title = kq2.Title;
                model.AboutCompany.Body = kq2.Body;
                model.AboutCompany.Description = kq2.Description;
                model.AboutCompany.PressId = kq2.PressId;
               
                
            }
            else
            {
                model.AboutCompany = null;
            }
            model.ImagesSlide = Repository.SlideImages();
            return View("~/Views/Home/Index.aspx", model);

          
        }
    
        public ActionResult DetailsImage(int id,string lang)
        {
           
            if(lang.Equals("en"))
                return View("~/Views/Home/TranslatedCopy/DetailsImage.aspx", Repository.FindImageById(id));
            return View("~/Views/Home/DetailsImage.aspx",Repository.FindImageById(id));
        }
        [HandleError]
        public ActionResult ViewPressDetails(int pressId,string lang)
        {
            
            if(lang=="vi")
            {
                var m = r.GetPress(pressId);
                m.ViewCount++;
                r.db.SaveChanges();
                PressDetailsViewModel model = new PressDetailsViewModel();
                model.Press = m;
                model.Presses = r.TopBaiVietTheoDanhMuc(m.PressCategoryId,8);
                return View("~/Views/Home/ViewPressDetails.aspx",model);
            }
            else
            {
                PressDetailsViewModel model = new PressDetailsViewModel();
                var t = r.GetPressTranslatedByPressId(pressId);

                if (t != null)
                    model.Press_TranslatedCopy = t;
                else
                {
                    model.Press_TranslatedCopy = r.GetPressTranslated(pressId);
                }
                model.Press_TranslatedCopy.ViewCount++;
                r.db.SaveChanges();
               
                model.Presses_TranslatedCopy = r.TopBaiDichTheoMaDanhMuc(model.Press_TranslatedCopy.Press.PressCategoryId,8);
                return View("~/Views/Home/TranslatedCopy/ViewPressDetails.aspx", model);
            }
        }
        public ActionResult ViewPressCategoryDetails(int danhmucId,string lang,int ?currentPage)
        {
            PressViewModel model = new PressViewModel();
            var ssq = r.GetPressCategory(danhmucId);
            model.PressCategoryName = ssq.Title;
            model.PressCategoryId = danhmucId;
            if(lang=="vi")
            {
                
                model.TotalItem = ssq.Presses.Count;

                if(currentPage.HasValue)
                {
                    model.PressList = r.PressesByCategory(currentPage.Value-1, ssq.PressCategoryId);
                    model.CurrentPage = currentPage.Value;
                }
                else
                {
                    model.PressList = r.PressesByCategory( 0, ssq.PressCategoryId);
                    model.CurrentPage = 1;
                    
                }
                return View("~/Views/Home/ViewPressCategoryDetails.aspx",model);

            }
            else
            {
             
                model.TotalItem = ssq.Presses.Count;

                if (currentPage.HasValue)
                {
                    model.Press_TranslatedCopyList = new List<Press_TranslatedCopy>();
                   var lst = r.PressesByCategory(currentPage.Value-1, ssq.PressCategoryId);
                    foreach(var item in lst)
                    {
                        var kq = r.GetPressTranslatedByPressId(item.PressId);
                        if(kq!=null)
                        model.Press_TranslatedCopyList.Add(kq);
                    }
                    model.CurrentPage = currentPage.Value;

                }
                else
                {
                    model.Press_TranslatedCopyList = new List<Press_TranslatedCopy>();
                    var lst = r.PressesByCategory(0, ssq.PressCategoryId);
                    foreach (var item in lst)
                    {

                        var kq = r.GetPressTranslated(item.PressId);
                        if (kq != null)
                            model.Press_TranslatedCopyList.Add(kq);
                    }
                    model.CurrentPage = 1;

                }
                return View("~/Views/Home/TranslatedCopy/ViewPressCategoryDetails.aspx",model);
            }
          
        }
        public ActionResult About(string lang)
        {
            if(lang=="en")
            {
                return View("~/Views/Home/EnglishCopy/About.aspx");
            }
            return View();
        }
    }
}
